<?php
namespace Modules\ShopApi\Models;
use Modules\ShopApi\Services\ShopUserService;
use Modules\AuthAdmin\Services\CommonService;
class ShopProductFootprint extends BaseApiModel
{
	/**
	 * @name  关联商品   一对一
	 */
	 public function product_one()
    {
        return $this->hasOne('Modules\ShopApi\Models\ShopProduct','id','product_id');
    }
	/**
	 * @name  商品足迹
	 */
	public function get_footprint(){
		$user_info = (new ShopUserService())->my();
		$list =	$this->where(['user_id'=>$user_info['id']])
				->select('id','product_id')
				->whereHas('product_one',function($query){
					$query->where('status',1);
				})
				->with([
					'product_one'=>function($query){
						$query->select('id','name','image_id','money','sales','bazaar_money');  
					},
					'product_one.image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->orderBy('created_at','desc')
				->paginate(10)
				->toArray();		
		$list['data'] = $this->get_footprint_data($list['data']);
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total']
		]]);
	}
	/**
	 * @name  商品足迹数据处理
	 */
	public function get_footprint_data($data){
		$arr = [];
		$CommonService = new CommonService();
		$http = $CommonService->get_http_type();
		foreach($data as $k=>$v){
			if(isset($v['product_one']['image_one']) && $v['product_one']['image_one']){
				if($v['product_one']['image_one']['open'] == 1){
					$v['product_one']['image_url'] = $http.$v['product_one']['image_one']['url'];
				}else{
					$v['product_one']['image_url'] = $v['product_one']['image_one']['url'];
				}
			}
			$arr[$k] = $v['product_one'];	
		}
		return $arr;
	}
}
